package com.src;

import modelos.Pelicula;

import com.src.base_de_datos.ManejadorDeBaseDeDatos;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SimpleCursorAdapter;

public class MustSeeActivity extends ListActivity {
    
	// ========================================================================
	private ManejadorDeBaseDeDatos 	manejador_bd;
	private EditText				texto_buscar_pelicula;
	private Button					boton_buscar_pelicula;
	
	public static final int 	ACTIVIDAD_BUSCAR_PELICULA 	= 0;
	// ========================================================================
	
    @Override
    // ------------------------------------------------------------------------
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.lista_peliculas);
        
        this.manejador_bd = new ManejadorDeBaseDeDatos(this);
        this.manejador_bd.abrir();

        definirComponentes();
        suscribirABotonBuscar();
        
        llenarDatos();
    }
    
    // ------------------------------------------------------------------------
    private void definirComponentes()
    {
    	this.texto_buscar_pelicula 	= (EditText) findViewById(R.id.texto_buscar_pelicula);
    	this.boton_buscar_pelicula	= (Button)findViewById(R.id.boton_buscar_pelicula);
    }
    
    // ------------------------------------------------------------------------
    private void suscribirABotonBuscar()
    {
   		this.boton_buscar_pelicula.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				buscarPelicula();
			}
		});
    }
    
    // ------------------------------------------------------------------------
    private void llenarDatos()
    {
        Cursor cursor = this.manejador_bd.obtenerTodosLosObjetos("Pelicula");
        startManagingCursor(cursor);
        
        String[] 	desde 	= new String[] 	{ Pelicula.CAMPO_TITULO };
        int[] 		a 		= new int[] 	{ R.id.texto_pelicula 	};
        
        SimpleCursorAdapter peliculas = new SimpleCursorAdapter(this, R.layout.fila_pelicula, cursor, desde, a);
        setListAdapter(peliculas);
    }
    
    // ------------------------------------------------------------------------
    private void buscarPelicula()
    {
    	Editable texto = (Editable)this.texto_buscar_pelicula.getText();
    	
    	if(textoValido(texto))
    	{
    		String texto_plano = texto.toString();
    		Intent i = new Intent(this, BuscarPelicula.class);
    		i.putExtra(Pelicula.CAMPO_TITULO, texto_plano);
    		
    		startActivityForResult(i, ACTIVIDAD_BUSCAR_PELICULA);
   		}
    	else
    	{
    		//Y.. no se.. tira un cartel
    	}
    }
    
    // ------------------------------------------------------------------------
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        Bundle extras = intent.getExtras();
        switch(requestCode) {
            case ACTIVIDAD_BUSCAR_PELICULA:
                String title = extras.getString(NotesDbAdapter.KEY_TITLE);
                String body = extras.getString(NotesDbAdapter.KEY_BODY);
                mDbHelper.createNote(title, body);
                fillData();
                break;
        }
    }

    // ------------------------------------------------------------------------
    private boolean textoValido(Editable texto)
    {
    	return true;
    }
    
    /*    @Override
    // ------------------------------------------------------------------------
    public boolean onCreateOptionsMenu(Menu menu) {
    	boolean result = super.onCreateOptionsMenu(menu);
        menu.add(0, INSERT_ID, 0, R.string.agregar_pelicula);
        return result;
    }

    @Override
    // ------------------------------------------------------------------------
    public boolean onOptionsItemSelected(MenuItem item) {
    	switch (item.getItemId()) {
        case INSERT_ID:
        	crearPelicula();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }*/
    

}